package com.ydl.zhwc.mvpandrxjava.sample.error;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.ydl.zhwc.mvpandrxjava.R;
import com.ydl.zhwc.mvpandrxjava.bean.HeWeather6;
import com.ydl.zhwc.mvpandrxjava.bean.Now;
import com.ydl.zhwc.mvpandrxjava.bean.Total;
import com.ydl.zhwc.mvpandrxjava.sample.RxUtils;
import com.ydl.zhwc.mvpandrxjava.sample.http.ApiProvider;
import com.ydl.zhwc.mvpandrxjava.sample.http.ApiService;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2017/12/22
 */

public class ErrorActivity extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);
        tv = findViewById(R.id.text_view);
    }

    public void onLoadClick(View view) {
        rxRequestNow2();
    }


    private void rxRequestNow() {
        ApiProvider.getApiService()
                .rxWeather(ApiService.KEY, "hangzhou")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HeWeather6>() {
                    @Override
                    public void accept(HeWeather6 heWeather6) throws Exception {
                        List<Total> totals = heWeather6.weather;
                        if (!totals.isEmpty() && totals.get(0).status.equals("ok")) {
                            tv.setText(totals.get(0).now.toString());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        tv.setText(throwable.getMessage());
                    }
                });
    }


    private void requestNow() {
        ApiProvider.getApiService()
                .weather(ApiService.KEY, "hangzhou")
                .enqueue(new Callback<HeWeather6>() {
                    @Override
                    public void onResponse(Call<HeWeather6> call, Response<HeWeather6> response) {
                        List<Total> totals = response.body().weather;
                        if (!totals.isEmpty() && totals.get(0).status.equals("ok")) {
                            tv.setText(totals.get(0).now.toString());
                        }
                    }

                    @Override
                    public void onFailure(Call<HeWeather6> call, Throwable t) {
                        tv.setText(t.getMessage());
                    }
                });
    }


    private void rxRequestNow2() {
        ApiProvider.getApiService()
                .rxWeather(ApiService.KEY, "hangzhou")
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.handlerThrowable())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Now>() {
                    @Override
                    public void accept(Now now) throws Exception {
                        tv.setText(now.toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        tv.setText(RxUtils.getErrorMsg(throwable));
                    }
                });

    }


}
